package html;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Element;

import data.ChatServerInfo;
import data.UserTable;

public class HtmlBuilder {
	
	public Element buildHtml(){
		Element root=new Element("html");
		Element body=new Element("body");
		Element center = new Element("center");
		
		ChatServerInfo info = ChatServerInfo.getInstance();
		
		
		Element start=new Element("a");
		start.setAttribute("style","font-size:25px");
		Element stop = new Element("a");
		stop.setAttribute("style","font-size:25px");
		Element time = new Element("p");
		Element refresh = new Element("a");
		refresh.setAttribute("href", "/");
		refresh.setAttribute("style","font-size:25px");
		refresh.setText("refresh");
		Element table = new Element("table");
		UserTable userTable = UserTable.getInstance();
		Element th = new Element("tr");
		Element cen = new Element("center");
		cen.setText("Online users");
		th.addContent(cen);
		table.addContent(th);
		
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (!info.isRunning()) {
			start.setAttribute("href", "?option=start");
			time.setText("stop time:"+format.format(new Date(info.getTime())));
		}else{
			stop.setAttribute("href", "?option=stop");
			time.setText("start time:"+format.format(new Date(info.getTime())));
			for (String name:userTable.getUers().keySet()) {
				Element tr = new Element("tr");
				Element center1 = new Element("center");
				center1.setText(name);
				tr.addContent(center1);
				table.addContent(tr);
			}
		}
		start.setText("start");
		stop.setText("stop");
		Element startP = new Element("p");
		Element stopP = new Element("p");
		Element refreshP = new Element("p");
		startP.addContent(start);
		stopP.addContent(stop);
		refreshP.addContent(refresh);
		
		center.addContent(startP);
		center.addContent(stopP);
		center.addContent(refreshP);
		center.addContent(time);
		center.addContent(table);
		body.addContent(center);
		root.addContent(body);
		return root;
	}

}
